package com.bj58.mis.apm.server.statistics.controller;

import com.bj58.mis.apm.server.core.common.result.ResultBean;
import com.bj58.mis.apm.server.statistics.dto.optimize.CubeScore;
import com.bj58.mis.apm.server.statistics.dto.optimize.CubeSuggest;
import com.bj58.mis.apm.server.statistics.dto.optimize.CuboidStats;
import com.bj58.mis.apm.server.statistics.dto.optimize.SuggestFactor;
import com.bj58.mis.apm.server.statistics.dto.rowkey.RowkeyStats;
import com.bj58.mis.apm.server.statistics.dto.warn.WarnInfo;
import com.bj58.mis.apm.server.statistics.service.OptimizeService;
import com.bj58.mis.apm.server.statistics.service.RowkeyStatsService;
import com.bj58.mis.apm.server.statistics.service.WarnService;
import com.bj58.mis.apm.server.statistics.suggest.TurningSuggest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/optimize")
@Api(tags = "调优相关API", description = "/optimize")
@Validated
public class CubeOptimizeController extends BaseController<OptimizeService> {

    @Resource
    private RowkeyStatsService rowkeyStatsService;

    @Resource
    private WarnService warnService;

    @Resource
    private TurningSuggest turningSuggest;

    /**
     * 查询指定cube的各指标得分
     * @param cubeId
     * @return
     */
    @ApiOperation(value = "Cube综合评分!", notes = "查询指定cube的各指标得分")
    @ApiImplicitParams({
            @ApiImplicitParam(name="cubeId", value = "Cube ID", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping(path="/score")
    public ResultBean<CubeScore> getScore(@NotNull @RequestParam(name="cubeId") String cubeId){

        return ResultBean.success(this.service.getScore(cubeId));
    }

    /**
     * 查询指定cuboid的数据情况
     * @param cubeId
     * @return
     */
    @ApiOperation(value = "cuboid数据情况!", notes = "查询指定cuboid的数据情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name="cubeId", value = "Cube ID", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping(path="/shrink")
    public ResultBean<CuboidStats> getShrink(@NotBlank @RequestParam(name="cubeId") String cubeId){

        return ResultBean.success(this.service.getShrink(cubeId));
    }

    /**
     * 优化建议
     * @param cubeId
     * @return
     */
    @ApiOperation(value = "优化建议情况", notes = "查询指定cube的优化建议")
    @ApiImplicitParams({
            @ApiImplicitParam(name="cubeId", value = "Cube ID", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping(path="/suggest")
    public ResultBean<CubeSuggest> getSuggest(@RequestParam(name="cubeId") String cubeId, @ModelAttribute SuggestFactor factor){
        return ResultBean.success(turningSuggest.getCubeSuggest(cubeId, factor));
    }

    @ApiOperation(value = "默认调优因子数值", notes = "查询默认调优因子数值")
    @GetMapping(path="/factor")
    public ResultBean<SuggestFactor> getSuggestFactor(){
        return ResultBean.success(turningSuggest.getSuggestFactor());
    }

    /**
     * 查询指定rowkey的使用情况
     * 根据cubeId，统计所有相关日志
     * @param cubeId
     * @return
     */
    @ApiOperation(value = "rowkey使用情况!", notes = "查询指定rowkey的使用情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name="cubeId", value = "Cube ID", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping(path="/rowkeyStats")
    public ResultBean<List<RowkeyStats>> getRowkeyStats(@RequestParam(name="cubeId", required = false) String cubeId){
        return ResultBean.success(rowkeyStatsService.getRowkeyStatsByCube(cubeId));
    }

    @ApiOperation(value = "获取最后一次构建的CubeId和ProjectId", notes = "获取最后一次构建的CubeId和ProjectId")
    @ApiImplicitParams({
            @ApiImplicitParam(name="cubeId", value = "Cube ID", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping(path="/lastCube")
    public ResultBean<Map<String, String>> getLastCube(@RequestParam(name="cubeId", required = false) String cubeId) {
        return ResultBean.success(this.service.getLastCube(cubeId, getLoginUserName()));
    }


    private static List<WarnInfo> list = new ArrayList<>();
    private static int num = 1;
    /**
     * 查询存储-总体情况
     * @return
     */
    @ApiOperation(value = "提醒", notes = "提醒列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="limit", value = "条数", paramType = "query", required = false, dataType = "int")
    })
    @GetMapping(path="/warn/list")
    public ResultBean<List<WarnInfo>> getWarnList(@RequestParam(name="limit", required = false) Integer limit){
        return ResultBean.success(warnService.getWarnList(getLoginUserName()));
    }

    @ApiOperation(value = "提醒查看", notes = "提醒查看")
    @GetMapping(path="/warn/lookover")
    public ResultBean<String> lookOver(){
        list.clear();
        num = 0;
        try {
            warnService.lookover(getLoginUserName());
            return ResultBean.success("Success");
        }catch (Exception e) {
            return ResultBean.error(e.getMessage());
        }
    }
}
